package com.henu.acmrating.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtils {
    public static String upload(MultipartFile file, String path) throws IOException {
        if (file.isEmpty()) {
            return null;
        }
        File saveFile = new File(path);
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
        String fileName = file.getOriginalFilename();  //获取文件名
        String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);  // 后缀名
        fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + suffixName;
        file.transferTo(new File(path + File.separator + fileName)); //将临时存储的文件移动到真实存储路径下
        fileName = "/api/img/" + fileName;
        return fileName;
    }
}
